package com.blockchain.securewallet.utils;

import org.bitcoinj.core.Base58;
import org.bitcoinj.core.Sha256Hash;

import java.util.Arrays;

/**
 * Created by Chain-Cloud on 2018/6/7.
 */

public class AddressValidity {

    public static boolean btcAddressValidity(String address) {
        try {
            byte[] decodeAddress = Base58.decode(address);
            byte[] checkSum = new byte[4];
            byte[] srcAddress = new byte[decodeAddress.length - 4];
            System.arraycopy(decodeAddress, decodeAddress.length - 4, checkSum, 0, 4);
            System.arraycopy(decodeAddress, 0, srcAddress, 0, decodeAddress.length - 4);

            byte[] firstSHA = Sha256Hash.hash(srcAddress);
            byte[] secondSHA = Sha256Hash.hash(firstSHA);
            if (Arrays.equals(Arrays.copyOfRange(secondSHA, 0, 4), checkSum)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

}
